package cn.stylefeng.guns.sys.modular.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.stylefeng.guns.base.auth.model.LoginUser;
import cn.stylefeng.guns.base.util.Keys;
import cn.stylefeng.guns.base.util.RedisUtil;
import cn.stylefeng.guns.sys.core.auth.cache.SessionManager;
import cn.stylefeng.guns.sys.core.constant.factory.ConstantFactory;
import cn.stylefeng.guns.sys.modular.api.service.ApiAuthService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.factory.UserFactory;
import cn.stylefeng.guns.sys.modular.system.mapper.UserMapper;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.guns.sys.modular.system.warpper.UserWrapper;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author txl
 * @version 1.0
 * @date 2020/8/5 16:22
 * @description
 */
@Service
public class ApiAuthServiceImpl implements ApiAuthService {
    @Resource
    private UserMapper userMapper;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private DictService dictService;
    @Autowired
    private RedisUtil redisUtil;
    @Resource
    private ConstantFactory constantFactory;

    @Override
    public ResponseData user(String account) {
        User user = userMapper.getByAccount(account);
        if (user == null) {
            return ResponseData.error("没有该用户");
        }

        LoginUser loginUser = UserFactory.createLoginUser(user);

        //用户角色数组
        Long[] roleArray = Convert.toLongArray(user.getRoleId());

        //如果角色是空就直接返回
        if (roleArray != null && roleArray.length != 0) {
            //获取用户角色列表
            List<Long> roleList = new ArrayList<>();
            List<String> roleNameList = new ArrayList<>();
            List<String> roleTipList = new ArrayList<>();
            for (Long roleId : roleArray) {
                roleList.add(roleId);
                roleNameList.add(ConstantFactory.me().getSingleRoleName(roleId));
                roleTipList.add(ConstantFactory.me().getSingleRoleTip(roleId));
            }
            loginUser.setRoleList(roleList);
            loginUser.setRoleNames(roleNameList);
            loginUser.setRoleTips(roleTipList);
        }


        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(loginUser);
        Map<String, Object> map = new UserWrapper(stringObjectMap).wrap();
        BeanUtil.copyProperties(map, loginUser);
        String positionName = constantFactory.getPositionName(loginUser.getId());
        loginUser.setPositionName(positionName);
        return ResponseData.success(loginUser);
    }

    @Override
    public ResponseData tokenUser(String username) {
        User user = userMapper.getByAccount(username);
        if (user == null) {
            return ResponseData.error("没有该用户");
        }
        return ResponseData.success(user);
    }

    @Override
    public ResponseData verifyToken(String token) {
        if (token != null) {
            try {
                String userName = redisUtil.getString(Keys.API_TOKEN + token);
                if (!StringUtils.isEmpty(userName)) {
                    String tokens = redisUtil.getString(Keys.API_USER_NAME + userName);
                    if (token.equals(tokens)) {
                        return ResponseData.success();
                    }
                }
            } catch (Exception e) {
                return ResponseData.error("网路异常");
            }
        }
        return ResponseData.error("token过期");
    }
}
